function PdfPreview(){
	FormalWindow.apply(this,["Preview"]);	
	this.htmlX.addClass("PdfPreview");	
	this.htmlX[0]["dta"] = this;
	this.previewBox = {"htmlX":$('<iframe class="preview-pane" width="100%" height="650" frameborder="0" style="position:relative;z-index:999" src=""></iframe>')};
	this.append(this.previewBox);
}
PdfPreview.prototype = Object.create(FormalWindow.prototype);
PdfPreview.prototype.constructor = PdfPreview;

PdfPreview.prototype.setReportPreview =function(psrc){
	this.htmlX.find('iframe.preview-pane').attr('src', psrc);	
};

function TileGrid(){
	Component.apply(this,['<div style="padding:0px" class="boxTileLine col-xs-12 TileGrid"></div>']);
	this.render = null;
	this.dataProvider = [];
    this.ind = 0;
    //this.winRep = null;
    this.itemChange = function(){};
    this.htmlX[0]["dta"] = this;
}
TileGrid.prototype.setRender = function(prender){
	this.render = prender;
};
TileGrid.prototype.setDataProvider = function(dta){
	this.dataProvider = dta;
};
TileGrid.prototype.setHeight = function(height){
	this.htmlX.find(".boxTileLine").css({"height":height+"px"});
};


TileGrid.prototype.refreshGridDate = function(){
    var dpt = this.dataProvider.length;	
    this.htmlX.find(".tilecellgrid").remove();	
    for(var z=0;z<dpt;z++){
            var item  =  this.dataProvider[z];
            this.render.apply(item,[]);
            /*
            var tmpTile = $('<div class="tilecellgrid media">');
            tmpTile[0]["ind"] = z;
            tmpTile.append(item.htmlX);
            this.htmlX.append(tmpTile);
            */	
            item.htmlX.addClass("tilecellgrid");
			//col-xs-12
            item.htmlX.css({"padding":"0px"});
            item.htmlX[0]["ind"] = z;
            this.htmlX.append(item.htmlX);	
    }	
    this.htmlX.find(".tilecellgrid").click(this.setSelectedItem);    
    //$('#btn_remove_'+this.uid).show();
    //$("#conteudo > a.btnsabapages:first.active").find(".pull-right").remove().end().append('<span class="pull-right badge">'+dpt+'</span>');
};

TileGrid.prototype.orderDesc = function(campo){
	this.dataProvider.orderDesc(campo);
	this.refreshGridDate();
};


TileGrid.prototype.getSelectedIndex = function(){
        return this.ind;
};


TileGrid.prototype.getSelectedItem = function(){
	//alert("iddd:"+this.ind);
        if(this.dataProvider.length < this.ind){
            this.ind = 0;
        }
        return this.dataProvider[this.ind];
};

TileGrid.prototype.setSelectedItem = function(){
    var gridP = $(this).parents(".TileGrid");
    gridP.find(".selectedLine").removeClass("selectedLine");
    $(this).addClass("selectedLine");     
    //alert("teste:"+parseInt(this.id.replace("ind_","")));
    gridP[0]["dta"]["ind"] = parseInt(this.ind);
    gridP[0]["dta"].itemChange();        
};


TileGrid.prototype.orderAsc = function(campo){
    this.dataProvider.orderAsc(campo);
	this.refreshGridDate();
};

TileGrid.prototype.refresh = function(){
    this.refreshGridDate();
};

function DataGrid(){
	Component.apply(this,['<table cellpadding="0" cellspacing="0" border="0"></table>']);
	this.htmlX.addClass("DataGrid table table-hover table-striped").append('<thead></thead><tbody></tbody>');
	//this.htmlX.addClass("DataGrid").append('<div class="boxLines"></div><div class="HBox tooltips"><label class="Text txtCount">0 reg</label><i class="Img toPDF icon-print"></i></div>');
	this.columns = [];
    this.printConfig = {};
	this.setPrintConfig({});
    //this.winRep = null;
	//this.htmlX.append('<i class="Img toPDF icon-print"></i>');
	
    //this.htmlX.find("i.toPDF").click($.proxy(this.printGrid,this));
    this.itemChange = function(){};
    this.htmlX[0]["dta"] = this;
}
DataGrid.prototype.setPrintConfig = function(config){	
	this.printConfig = $.extend({"title":"Relatorio","subtitle":"relatorio"}, config);	
};
DataGrid.prototype.printGrid = function(){	
	if(!window["pdfpreview"]){
		window["pdfpreview"] = new PdfPreview();		
	}	
	try{
		pdfpreview.setReportPreview(toPDF({"data":this.getDataProvider(),"render":this.columns,"title":this.printConfig["title"],"subtitle":this.printConfig["subtitle"]}));
	}catch(e){		
		$.getScript('lib/jspdf/libspdf.js',$.proxy(function(){
			pdfpreview.setReportPreview(toPDF({"data":this.getDataProvider(),"render":this.columns,"title":this.printConfig["title"],"subtitle":this.printConfig["subtitle"]}));
		},this));   
	}
	pdfpreview.show(true,true);
};
DataGrid.prototype.setColumns = function(pcolumns){	
	this.columns = pcolumns;
};

DataGrid.prototype.getAoColumns = function(){	
	var ctm = this.columns.length;	
	var aoColuns = [];
    for(var z=0;z<ctm;z++){	  
		if(this.columns[z]["width"]!=0){
			aoColuns[z] = {"mData":this.columns[z].column,"sTitle":this.columns[z].label};
		}  	 
    	
    }
	return aoColuns;
};


DataGrid.prototype.getDataProvider = function(){
	return this.htmlX.dataTable().fnGetData();
};
DataGrid.prototype.getDataProviderCopy = function(){
	return  $.extend(true,[],this.getDataProvider());
};

DataGrid.prototype.setHeight = function(height){
	this.htmlX.find("tbody").css({"height":height+"px"});
};


DataGrid.prototype.setDataProvider = function(dta){
    if(!this.htmlX.hasClass("alredyGrided")){    	
    	this.htmlX.addClass("alredyGrided").dataTable({
    			"dom": '<"toolsgrid">ftip',
    			"bServerSide": false,
    			"sAjaxSource": null,
    			"aaData":dta,    			
    			"bInfo": false,    			
    			"aoColumns":this.getAoColumns(),
    			"bRetrieve": true
    		}).fnSort([[0,'desc']]);
    		$("#uid_"+this.uid+" tbody").on('click','tr',function(){	
    			var gridP = $(this).parents(".DataGrid");	
    			gridP.find(".selectedLine").removeClass("selectedLine");
    			$(this).addClass("selectedLine");		
    			gridP[0]["dta"].itemChange();
    		});    	
    	    $("#uid_"+this.uid+"_wrapper div.toolsgrid").addClass("btn-group").css({"margin-bottom":"10px"}).append('<a href="#" class="btn btn-default glyphicon iconeinput glyphicon-print">').click($.proxy(this.printGrid,this));   
    }else{    	
    	this.htmlX.dataTable().fnClearTable();
    	if(dta.length >0){
    		this.htmlX.dataTable().fnAddData(dta);
    	}    	
    	this.htmlX.dataTable().fnDraw(false);    	
    }
};

DataGrid.prototype.getSelectedIndex = function(){
	var indtmp = this.htmlX.dataTable().fnGetPosition( this.htmlX.find("tbody tr.selectedLine")[0] );
	return parseInt(indtmp);
};
DataGrid.prototype.updateItem = function(itemS,pindice){
	if(this.getSelectedItem()!=null){
		this.htmlX.dataTable().fnUpdate( itemS,this.htmlX.find("tbody tr.selectedLine")[0]);
	}	
};

DataGrid.prototype.addItem = function(itemS){
	this.htmlX.dataTable().fnAddData(itemS); // Row
};
DataGrid.prototype.removeItem = function(pindice){
	var indice = pindice||this.getSelectedIndex();
	this.htmlX.dataTable().fnDeleteRow(indice);
};

DataGrid.prototype.getSelectedItem = function(p_withArray){
	//var withArray = p_withArray||true;
	p_withArray = typeof p_withArray !== 'undefined' ? p_withArray : true;
	var itemS = this.htmlX.find("tbody tr.selectedLine");
	if(itemS.length > 0){
		if(p_withArray){
			return this.htmlX.dataTable().fnGetData(itemS[0]);
		}else{
			var itemTmpS = this.htmlX.dataTable().fnGetData(itemS[0]);
			var mybojSTmp = {};
			$.each( itemTmpS, function( key, val ) {
				    if(!$.isArray(itemTmpS[key])){
				    	mybojSTmp[key] = val;
				 	}
			});
			return mybojSTmp;
		}
		
	}
	return null;
	
};
DataGrid.prototype.getSelectedItemCopy = function(){
	var itemSTmp = this.getSelectedItem();
	if(itemSTmp != null){
		return $.extend(true,{},itemSTmp);
	}
	return null;
};

DataGrid.prototype.setSelectedItem = function(index){
	this.htmlX.find(".selectedLine").removeClass("selectedLine");
	this.htmlX.find("tbody tr:eq("+(index)+")").addClass("selectedLine");
};
DataGrid.prototype.refresh = function(){};
